<script>
	import {
		mapState
	} from 'vuex'
	import {
		verionUpdate
	} from 'api/common'
	export default {
		onLaunch: async function() {
			console.log('App Launch')
			// #ifdef APP-PLUS
			await this.checkVerionUpdate()
			// #endif
			// this.checkNetWorkStatus()
			this.isOpenLastRead()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		computed: {
			...mapState({
				readSetting: state => state.setting.readSetting,
				lastReadBookInfo: state => state.read.lastReadBookInfo
			}),
		},
		methods: {
			// 启动跳转最近阅读
			isOpenLastRead() {
				if (this.readSetting.openLastRead && this.lastReadBookInfo.bookId && this.lastReadBookInfo.chapterId) {
					uni.reLaunch({
						url: `/pages/read/index?bookId=${this.lastReadBookInfo.bookId}&chapterId=${this.lastReadBookInfo.chapterId}`
					})
				}
			},
			// 检查更新
			checkVerionUpdate() {
				return new Promise((resolve) =>{
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						let header = {
							version: wgtinfo.version,
							osType: 1
						}
						verionUpdate(header).then(res => {
							console.log(res.data.updateInfo)
							if (res.data.updateInfo.is_update == 1) {
								// 更新
								uni.navigateTo({
									url: `/pages/center/update?update_type=${res.data.updateInfo.update_type}&update_url=${res.data.updateInfo.update_url}&update_title=${res.data.updateInfo.update_title}&update_content=${res.data.updateInfo.update_content}`
								})
							}
							
							resolve()
						})
					});
				})
			},
			// 监测网络状态
			checkNetWorkStatus(){
				uni.onNetworkStatusChange(function(res) {
				    if(!res.isConnected){
				    	console.log('nonet')
				    	uni.navigateTo({
				    		url:'/pages/error/nonet'
				    	})
				    }else{
						console.log('onnet')
						uni.navigateBack({
							delta:1
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	/*每个页面公共css */
</style>
